package com.qdlc.p2p.biz.model;

import java.util.HashMap;
import java.util.Map;

import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.common.constant.Constant;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.PropertiesUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.common.util.code.MD5;
import com.qdlc.p2p.dal.dto.User;

public class AppResetPwdModel {

	/** 用户id */
	private long userId;

	/** 当前密码 */
	private String pwd;

	/** 新密码 */
	private String newPwd;

	/** type两个值（userpwd，paypwd，前者表示接口处理修改登录密码，后者处理支付密码 */
	private String type;

	private Map<String, Object> data;

	/**
	 * (忘记密码)重置密码
	 * 
	 * @return
	 */
	public Map<String, Object> resetPwdResponse() {
		UserService userService = (UserService) BeanUtil.getBean("userService");
		User user = new User();
		user.setUserId(getUserId());
		user.setPwd(getNewPwd());
		User modifyUser = userService.modifyPwd(user);
		if (StringUtil.isBlank(modifyUser)) {
			data.put("resCode", "08");
			data.put("resMsg", PropertiesUtil.getValue("UpdatePassword08"));
		} else {
			data.put("resCode", "1");
			data.put("resMsg", PropertiesUtil.getValue("UpdatePassword09"));
		}
		return data;
	}

	/**
	 * 校验用户
	 */
	public void validModel(User user) {
		if (StringUtil.isBlank(user)) {
			throw new UserException("不存在该用户！");
		}
	}

	/**
	 * (忘记密码)找回密码参数校验
	 * 
	 * @return
	 */
	public Map<String, Object> validParams() {
		if (StringUtil.isBlank(getUserId())) {
			data.put("resCode", "01");
			data.put("resMsg", PropertiesUtil.getValue("UpdatePassword01"));
			return data;
		}
		if (StringUtil.isBlank(getNewPwd())) {
			data.put("resCode", "03");
			data.put("resMsg", PropertiesUtil.getValue("UpdatePassword03"));
			return data;
		}
		return null;
	}

	/**
	 * 参数非空判断
	 * 
	 * @return
	 */
	public Map<String, Object> validParam() {
		data = new HashMap<String, Object>();
		if (StringUtil.isBlank(getUserId())) {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppResetPwd01"));
			return data;
		}
		if (StringUtil.isBlank(getPwd())) {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppResetPwd02"));
			return data;
		}
		if (StringUtil.isBlank(getNewPwd())) {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppResetPwd03"));
			return data;
		}
		if (StringUtil.isBlank(getType())) {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppResetPwd04"));
			return data;
		}
		return null;
	}

	/**
	 * 校验用户正确性
	 * 
	 * @return
	 */
	public Map<String, Object> validUser(User user) {
		String pwdParam = MD5.encode(getPwd());
		if (Constant.PWD_TYPE.equals(getType())) { // 修改用户密码
			String uPwd = user.getPwd();
			if (StringUtil.isBlank(pwdParam) || !uPwd.equals(pwdParam)) { // 当前密码与保存的密码不一致
				data.put("resCode", "0");
				data.put("resMsg", PropertiesUtil.getValue("AppResetPwd06"));
				return data;
			}
		} else { // 修改支付密码
			String payPwd = user.getPayPwd();
			if (StringUtil.isBlank(pwdParam) || !payPwd.equals(pwdParam)) { // 当前支付密码与保存密码不一致
				data.put("resCode", "0");
				data.put("resMsg", PropertiesUtil.getValue("AppResetPwd06"));
				return data;
			}
		}
		return null;
	}

	/**
	 * 
	 * @return
	 */
	public Map<String, Object> updatePwdResponse(User user, String pwdType) {
		data = new HashMap<String, Object>();
		UserService userService = (UserService) BeanUtil.getBean("userService");
		if (Constant.PWD_TYPE.equals(pwdType)) { // 用户密码修改
			user.setPwd(getNewPwd());
			userService.modifyPwd(user);
		} else { // 支付密码修改
			user.setPayPwd(getNewPwd());
			userService.modifyPaypwd(user);
		}
		data.put("resCode", "1");
		data.put("resMsg", PropertiesUtil.getValue("AppResetPwd05"));
		return data;
	}

	/**
	 * 
	 * @param user
	 * @return
	 */
	public Map<String, Object> validResult(User user) {
		data = new HashMap<String, Object>();
		if (!StringUtil.isBlank(user)) {
			data.put("resCode", "1");
			data.put("resMsg", PropertiesUtil.getValue("1"));
		} else {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("0"));
		}
		return data;
	}

	public long getUserId() {
		return userId;
	}

	public void setUserId(long userId) {
		this.userId = userId;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}
}
